package cm.com.carlamanda.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cm.com.carlamanda.domain.Page;
import cm.com.carlamanda.domain.Project;
import cm.com.carlamanda.service.ProjectService;
import cm.com.carlamanda.util.StringUtil;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping(value="project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    
    @RequestMapping(value="/projects",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject searchProject(@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,String sortColumn,String sortAsc){
        JSONObject jsonObject = new JSONObject();
        Page pa=new Page();
        pa.setPage(page);
        pa.setPageSize(pageSize);
        if(StringUtil.isNotEmpty(sortColumn)){
            pa.setSortColumn(sortColumn);
            pa.setSortAsc("desc".equalsIgnoreCase(sortAsc)?"desc":"asc");
        }
        List<Project> list=projectService.search(pa);
        jsonObject.put("page", pa);
        jsonObject.put("list",list);
        jsonObject.put("result",true);
        return jsonObject;
    }
    @RequestMapping(value="/project/{id}",method=RequestMethod.GET)
    @ResponseBody
    public JSONObject getProject(@PathVariable("id") String id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",true);
        Project project =new Project();
        project.setId(id);
        projectService.getProject(project);
        return jsonObject;
    }
    @RequestMapping(value="add",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject addProject(@RequestBody Project project){
        JSONObject jsonObject = new JSONObject();
        projectService.addProject(project);
        jsonObject.put("result",true);
        return jsonObject;
    }
    
    @RequestMapping(value="del",method=RequestMethod.POST)
    @ResponseBody
    public JSONObject delProject(@RequestBody Project project){
        JSONObject jsonObject = new JSONObject();
        projectService.delProject(project);
        jsonObject.put("result",true);
        return jsonObject;
    }
}
